// my/my.js
import {
  MyModel
} from '../../models/my'
import {
  config
} from '../../config.js'
const myModel = new MyModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {
      'nickName': '',
      'avatar': ''
    }, //支付宝头像及昵称
    swiper: [{
      path: '/assets/banner.jpg',
    }], // 轮播图
    firstClick: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadUserInfo()
    app.myLogin().then(res => {
      this.loadMyAccount()
      my.hideLoading();
    })
  },
  // 获取支付宝头像名称
  loadUserInfo() {
    app.getUserInfo().then(
      user => {
        this.setData({
          user,
        });
      },
      () => {
        // 获取用户信息失败
      }
    );
  },
  // 获取账户信息
  loadMyAccount() {
    myModel.loadMyAccount().then(res => {
      console.log(res)
      if (res) {
        if (res.code == '00') {
          switch (res.result.customerLevel) {
            case 1:
              res.result.customerLeveTxt = '普通客户'
              break;
            case 10:
              res.result.customerLeveTxt = '金卡客户'
              break;
            case 20:
              res.result.customerLeveTxt = '铂金卡客户'
              break;
            case 30:
              res.result.customerLeveTxt = '钻石卡客户'
              break;
            case 40:
              res.result.customerLeveTxt = '出租车/专快车'
              break;
            case 50:
              res.result.customerLeveTxt = '壳牌中国员工'
              break;
          }
          this.setData({
            my: res.result
          })
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        this.setData({
          openTipPop: true,
          TipTxt: '获取账户信息失败',
          tips: 'close',
        });
      }
      
    })
  },
  // 跳转
  toPage(e) {
    const page = e.currentTarget.dataset.url
    if (page== 'invoice' || page== '/pages/data/data/data'){
      my.alert({
        title: '很抱歉',
        content: '该功能暂未开通，敬请期待~',
        buttonText: '我知道了',
      });
    } else {
      my.navigateTo({
        url: page
      })
    }
  },
  goCard() {
    my.addCardAuth({
      url: config.cardlink,
      //  测试
      // url: 'https://memberprod.alipay.com/account/openform/activecard.htm?app_id=2019062465644921&template_id=20200324000000002196014000300468&__webview_options__=canPullDown%3dNO%26transparentTitle%3dauto&out_string=openCard,BJSHELL001',
      // 生产
      // url: 'https://memberprod.alipay.com/account/openform/activecard.htm?app_id=2019112769468287&template_id=20200325000000002197957000300105&__webview_options__=canPullDown%3dNO%26transparentTitle%3dauto&out_string=openCard,BJSHELL001&callback=https://zhifubao.bjshell.com.cn/AlipayPortal/d/alipay/bjShopPassed',
      success: (res) => {
        // my.alert({content: res});
        this.notifyBjOpenCard(res.result)
      },
      fail: (res) => {
        // my.alert({content: '授权失败'});
      },
    });
  },
  notifyBjOpenCard(result) {
    my.request({
      url: config.api_bais + 'd/program/marketingCardService/notifyBjOpenCard',
      data: {
        scope: result.scope,
        template_id: result.template_id,
        state: result.state,
        app_id: result.app_id,
        request_id: result.request_id,
        out_string: result.out_string,
        auth_code: result.auth_code,
      },
      method: 'POST',
      headers: {
        'content-type': 'application/json'
      },
      success: res => {
        if (res.data && res.data.data &&  res.data.data.code && res.data.data.code == '00') {
          my.alert({content: '领取会员卡成功，可在支付宝首页-右上角卡包中查看！'});
        } else {
          my.alert({content: '领取会员卡失败，请稍后再试'});
        }
      },
      fail: (error) => {
        my.alert({content: '连接失败，请重试！'});
        console.log('fail')
      },

    })
  },
  // 点击知道了
  tipPopSure(tips) {
    this.setData({
      openTipPop: false
    });
    if (tips == 'href') {
      my.navigateBack('1')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})